home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import os
- import re
- from rdflib.Literal import Literal
- from rdflib.BNode import BNode
- from rdflib.URIRef import URIRef
- from rdflib.sparql.graphPattern import _createResource
- from rdflib.sparql import _questChar, Debug
- from rdflib.sparql.Unbound import Unbound
-
- def queryString(v):
- if isinstance(v, basestring) and len(v) != 0:
- pass
- return v[0] == _questChar
-
-
- def getLiteralValue(v):
- return v
-
-
- def getValue(param):
-
- def f(bindings):
- if unBound:
- val = bindings[param]
- if isinstance(val, Literal):
- return getLiteralValue(val)
- return val
- unBound
- return value
-
- return f
-
-
- def lt(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) < fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def le(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) <= fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def gt(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) > fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def ge(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) >= fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def eq(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) == fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def neq(a, b):
- fa = getValue(a)
- fb = getValue(b)
-
- def f(bindings):
-
- try:
- return fa(bindings) != fb(bindings)
- except:
- if Debug:
- (typ, val, traceback) = sys.exc_info()
- sys.excepthook(typ, val, traceback)
-
- return False
-
-
- return f
-
-
- def __getQueryString(v):
- if isinstance(v, Unbound):
- return v.name
- if queryString(v):
- return v
- return None
-
-
- def bound(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return False
- if v in bindings:
- val = bindings[v]
- return not (val == None)
- return False
-
- return f
-
-
- def isURI(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return False
-
- try:
- val = bindings[v]
- if val == None:
- return False
- return isinstance(val, URIRef)
- except:
- v == None
- return False
-
-
- return f
-
-
- def isIRI(a):
- return isURI(a)
-
-
- def isBlank(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return False
-
- try:
- val = bindings[v]
- if val == None:
- return False
- return isinstance(val, BNode)
- except:
- v == None
- return False
-
-
- return f
-
-
- def isLiteral(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return False
-
- try:
- val = bindings[v]
- if val == None:
- return False
- return isinstance(val, Literal)
- except:
- v == None
- return False
-
-
- return f
-
-
- def str(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return ''
-
- try:
- val = bindings[v]
- if val == None:
- return ''
- return `val`
- except:
- v == None
- return ''
-
-
- return f
-
-
- def lang(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- return ''
-
- try:
- val = bindings[v]
- if val == None:
- return ''
- return val.lang
- except:
- v == None
- return ''
-
-
- return f
-
-
- def datatype(a):
- v = __getQueryString(a)
-
- def f(bindings):
- if v == None:
- if isinstance(a, Literal):
- return a.datatype
- return ''
- v == None
-
- try:
- val = bindings[v]
- if val == None:
- return ''
- return val.datatype
- except:
- return ''
-
-
- return f
-
-
- def isOnCollection(collection, item, triplets):
- """Generate a method that can be used as a global constaint in sparql to check whether
- the 'item' is an element of the 'collection' (a.k.a. list). Both collection and item can
- be a real resource or a query string. Furthermore, item might be a plain string, that is
- then turned into a literal run-time.
- The method returns an adapted method.
- """
- collUnbound = False
- if isinstance(collection, Unbound):
- collUnbound = True
- collection = collection.name
- elif queryString(collection):
- collUnbound = True
- else:
- collUnbound = False
- if isinstance(item, Unbound):
- queryItem = item.name
- itUnbund = True
- elif queryString(item):
- queryItem = item
- itUnbound = True
- else:
- queryItem = _createResource(item)
- itUnbound = False
-
- def checkCollection(bindings):
-
- try:
- if collUnbound == True:
- coll = bindings[collection]
- else:
- coll = collection
- if itUnbound == True:
- it = bindings[queryItem]
- else:
- it = queryItem
- return it in triplets.items(coll)
- except:
- return True
-
-
- return checkCollection
-
-
- def addOperator(args, combinationArg):
- '''
- SPARQL numeric + operator implemented via Python
- '''
- return []([ 'sparqlOperators.getValue(%s)%s' % (i, '') for i in args ])
-
-
- def XSDCast(source, target = None):
- """
- XSD Casting/Construction Support
- For now (this may be an issue since Literal doesn't override comparisons) it simply creates
- a Literal with the target datatype using the 'lexical' value of the source
- """
- sFunc = getValue(source)
-
- def f(bindings):
- rt = sFunc(bindings)
- if isinstance(rt, Literal) and rt.datatype == target:
- return rt
- return Literal(rt, datatype = target)
-
- return f
-
-
- def regex(item, pattern, flag = None):
- '''
- Invokes the XPath fn:matches function to match text against a regular expression pattern.
- The regular expression language is defined in XQuery 1.0 and XPath 2.0 Functions and Operators section 7.6.1 Regular Expression Syntax
- '''
- a = getValue(item)
- b = getValue(pattern)
- if flag:
- cFlag = 0
- usedFlags = []
- for fChar, _flag in [
- ('i', re.IGNORECASE),
- ('s', re.DOTALL),
- ('m', re.MULTILINE)]:
- if fChar in flag and fChar not in usedFlags:
- cFlag |= _flag
- usedFlags.append(fChar)
- continue
-
-
- def f1(bindings):
-
- try:
- return bool(re.compile(b(bindings), cFlag).search(a(bindings)))
- except:
- return False
-
-
- return f1
-
- def f2(bindings):
-
- try:
- return bool(re.compile(b(bindings)).search(a(bindings)))
- except:
- return False
-
-
- return f2
-
- def f(bindings):
-
- try:
- print '%s %s' % (a(bindings), b(bindings))
- return bool(re.compile(a(bindings)).search(b(bindings)))
- except Exception:
- e = None
- print e
- return False
-
-
- return f
-
-